package com.example.bailin.giftsay.sort;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.main.SearchActivity;
import com.example.bailin.giftsay.main.SelectActivity;
import com.example.bailin.giftsay.sort.adapter.SortAdapter;
import com.example.bailin.giftsay.tool.MyApp;

import java.util.ArrayList;

/**
 * Created by bailin on 16/8/15.
 *
 * 分类的主Fragment
 */
public class SortFragment extends BaseFragment implements View.OnClickListener {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private TextView tvXlsq;
    private RelativeLayout rlSearch;

    @Override
    protected int setLayout() {
        return R.layout.fragment_sort;
    }

    @Override
    protected void initView() {
        rlSearch = bindView(R.id.rl_search_sort_id);
        tabLayout = bindView(R.id.tablayout_sory_id);
        viewPager = bindView(R.id.vp_sort_id);
        tvXlsq = bindView(R.id.tv_xlsq);
        tvXlsq.setOnClickListener(this);
        rlSearch.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        tabLayout.setTabTextColors(Color.WHITE, Color.WHITE);
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<String> strings = new ArrayList<>();
        fragments.add(new StrategySortFragment());
        fragments.add(new SingleSortFragment());
        strings.add("攻略");
        strings.add("单品");

        SortAdapter adapter = new SortAdapter(getChildFragmentManager());
        adapter.setStrings(strings);
        adapter.setFragments(fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        // ViewPager翻页显示选礼神器
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    tvXlsq.setVisibility(View.VISIBLE);
                } else {
                    tvXlsq.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_search_sort_id:
                Intent intent = new Intent(MyApp.getContext(), SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_xlsq:
                Intent intent1 = new Intent(MyApp.getContext(), SelectActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
